package edu.neu.swc.commerce.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.commerce.service.NacosClientService;
import edu.neu.swc.commerce.service.communication.AuthorityFeignClient;
import edu.neu.swc.commerce.service.communication.UseFeignApi;
import edu.neu.swc.commerce.service.communication.UseRestTemplateService;
import edu.neu.swc.commerce.service.communication.UseRibbonService;
import edu.neu.swc.commerce.service.hystrix.NacosClientHystrixCommand;
import edu.neu.swc.commerce.vo.JwtToken;
import edu.neu.swc.commerce.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import rx.Observable;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * <h1>微服务通信Controller</h1>
 */
@RestController
@Slf4j
@RequestMapping("/communication")
public class CommunicationController {

    private final UseRestTemplateService restTemplateService;

    private final UseRibbonService ribbonService;

    private final AuthorityFeignClient feignClient;

    private final UseFeignApi feignApi;

    public CommunicationController(UseRestTemplateService restTemplateService,
                                   UseRibbonService useRibbonService,
                                   AuthorityFeignClient feignClient,
                                   UseFeignApi feignApi) {
        this.restTemplateService = restTemplateService;
        this.ribbonService = useRibbonService;
        this.feignClient = feignClient;
        this.feignApi = feignApi;
    }

    @PostMapping("/rest-template")
    public JwtToken getTokenFromAuthorityService(
            @RequestBody UsernameAndPassword usernameAndPassword){
        return restTemplateService.getTokenFromAuthorityService(usernameAndPassword);
    }

    @PostMapping("/rest-template-load-balance")
    public JwtToken getTokenFromAuthorityServiceWithLoadBalance(
            @RequestBody UsernameAndPassword usernameAndPassword){
        return restTemplateService.getTokenFromAuthorityServiceWithLoadBalance(usernameAndPassword);
    }

    @PostMapping("/ribbon")
    public JwtToken getTokenFromAuthorityServiceByRibbon(
            @RequestBody UsernameAndPassword usernameAndPassword){
        return ribbonService.getTokenFromAuthorityServiceByRibbon(usernameAndPassword);
    }

    @PostMapping("/thinking-in-ribbon")
    public JwtToken thinkingInRibbon(
            @RequestBody UsernameAndPassword usernameAndPassword){
        return ribbonService.thinkingInRibbon(usernameAndPassword);
    }

    @PostMapping("/token-by-feign")
    public JwtToken getTokenByOpenFeign(
            @RequestBody UsernameAndPassword usernameAndPassword){
        return feignClient.getTokenByFeign(usernameAndPassword);
    }

    @PostMapping("/thinking-in-feign")
    public JwtToken thinkingInFeign(
            @RequestBody UsernameAndPassword usernameAndPassword){
        return feignApi.thinkingInFeign(usernameAndPassword);
    }
}

